package com.wlit.print;

import java.io.*;
import java.util.Properties;

public class PrintConfig {

    private static String FILE_NAME = "laberprinter.properties";

    private Properties properties;
    private String comName;

    private static PrintConfig instance = null;

    public static PrintConfig getInstance(){
        if (instance == null) {
            instance = new PrintConfig();
        }
        return instance;
    }

    public void init() {
        InputStream in = null;
        try {
            properties = new Properties();
            in = new FileInputStream(System.getProperty("user.dir")+ File.separator+FILE_NAME);
            properties.load(in);
            comName = properties.getProperty("printer.com");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (in != null){
                try {
                    in.close();
                }catch (Exception ex){}
            }
        }
    }

    public String getComName() {
        return comName;
    }

    public void save(String comName) {
        properties.setProperty("printer.com", comName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(System.getProperty("user.dir")+ File.separator+FILE_NAME);
            properties.store(out, "laber printer serial port");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null){
                try {
                    out.close();
                }catch (Exception ex){}
            }
        }
    }
}
